using System;
using System.Collections.Generic;
using System.Text;

namespace net.notdot.Attercop
{
    class FilteredEnumerator<T> : IEnumerator<T>
    {
        public delegate bool FilterDelegate(T arg);

        IEnumerator<T> baseEnumerator;
        FilterDelegate filter;

        public FilteredEnumerator(IEnumerator<T> baseEnum, FilterDelegate filter)
        {
            baseEnumerator = baseEnum;
            this.filter = filter;
        }

        #region IEnumerator<T> Members

        public T Current
        {
            get
            {
                return baseEnumerator.Current;
            }
        }

        #endregion

        #region IDisposable Members

        public void Dispose()
        {
            baseEnumerator.Dispose();
        }

        #endregion

        #region IEnumerator Members

        object System.Collections.IEnumerator.Current
        {
            get {
                return (object)baseEnumerator.Current;
            }
        }

        public bool MoveNext()
        {
            bool ret;
            while ((ret = baseEnumerator.MoveNext()) && !filter(Current)) ;

            return ret;
        }

        public void Reset()
        {
            baseEnumerator.Reset();
        }

        #endregion
    }
}
